﻿using System.Collections.Generic;
using System.Drawing;

namespace DotNet.Common
{
    public class LinePoints
    {
        List<Point> _points = new List<Point>();
        Point _lastPoint;
        Point _addPoint;

        public Point[] Points
        {
            get
            {
                return _points.ToArray();
            }
        }

        public void Begin()
        {
            _points.Clear();
            _addPoint = _lastPoint = Point.Empty;
        }

        public void Add(Point p)
        {
            if (_points.Count == 0)
            {
                AddPoint(p);
            }
            else if (p.Y != _lastPoint.Y)
            {
                AddPoint(_lastPoint);
                AddPoint(p);
            }
            _lastPoint = p;
        }

        public void End()
        {
            if (!_lastPoint.IsEmpty)
            {
                AddPoint(_lastPoint);
            }
        }

        private void AddPoint(Point p)
        {
            if (_addPoint == p)
                return;

            _points.Add(p);
            _addPoint = p;
        }
    }
}
